package com.ridaizu.dao.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import com.ridaizu.dao.DaoAdapter;
import com.ridaizu.dao.db.DBAccess;
import com.ridaizu.model.Model;
import com.ridaizu.model.TransitDetail;
import com.ridaizu.model.search.SearchModel;

@ManagedBean(name = "transitDetailDao")
@ApplicationScoped
public class TransitDetailDao extends DaoAdapter {

	private static final long serialVersionUID = 1L;

	public int selectCount(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from d_transit_detail \n");
		sql.append(searchModel.getSelectCountSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		if (rs == null) {
			return -1;
		}

		rs.next();

		return rs.getInt(1);
	}

	public List<TransitDetail> selectTransitDetailList(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("SELECT \n");
		sql.append("td.id as id, \n");
		sql.append("td.transit_id as transit_id, \n");
		sql.append("t.transit_date as transit_date, \n");
		sql.append("td.goods_id as goods_id, \n");
		sql.append("g.name as goods_name, \n");
		sql.append("td.count as count, \n");
		sql.append("td.price as price \n");
		sql.append("FROM d_transit t, d_transit_detail td, m_goods g \n");
		sql.append(searchModel.getSelectSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		List<TransitDetail> modelList = new ArrayList<TransitDetail> ();
		while (rs.next()) {
			modelList.add((TransitDetail)getModel(rs));
		}

		rs.close();

		return modelList;
	}

	@Override
	public Model getModel(ResultSet rs) throws SQLException {

		checkNotNull(rs);
		TransitDetail transitDetail = new TransitDetail();
		transitDetail.setId(rs.getInt("id"));
		transitDetail.setTransitId(rs.getInt("transit_id"));
		transitDetail.setTransitDate(rs.getTimestamp("transit_date"));
		transitDetail.setGoodsId(rs.getInt("goods_id"));
		transitDetail.setGoodsName(rs.getString("goods_name"));
		transitDetail.setCount(rs.getInt("count"));
		transitDetail.setPrice(rs.getDouble("price"));

		return transitDetail;
	}

	@Override
	public int insert(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO d_transit_detail \n");
		sql.append("( \n");
		sql.append("transit_id, goods_id, count, price) \n");
		sql.append("VALUES \n");
		sql.append("(?, ?, ?, ?); \n");

		TransitDetail transitDetail = (TransitDetail) model;

		Object[] params = new Object[] {
				transitDetail.getTransitId(), transitDetail.getGoodsId(),
				transitDetail.getCount(), transitDetail.getPrice()
		};

		return DBAccess.update(sql.toString(), params);
	}

	@Override
	public int update(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE d_transit_detail \n");
		sql.append("SET \n");
		sql.append("transit_id = ?, \n");
		sql.append("goods_id = ?, \n");
		sql.append("count = ?, \n");
		sql.append("price = ? \n");
		sql.append("WHERE id = ? \n");

		TransitDetail transitDetail = (TransitDetail) model;

		Object[] params = new Object[] {
				transitDetail.getTransitId(), transitDetail.getGoodsId(),
				transitDetail.getCount(), transitDetail.getPrice(), transitDetail.getId()
		};

		return DBAccess.update(sql.toString(), params);
	}

	public int delete(int id) {
		String sql = "DELETE FROM d_transit_detail WHERE id=?;";

		return DBAccess.update(sql.toString(), id);
	}

}
